package com.doit.day06

import scala.util.Random

/**
 * switch   case ...case ... default
 */
object _11_模式匹配 {
  def main(args: Array[String]): Unit = {
    //匹配值
    val str = "前进"

    str match {
      case "韩博" => println("我是韩博,不是前进")
      case "小杨" => println("我是美女，前进是个啥？？？")
      case "前进" => println("终于匹配上我了")
      case  _  => println("哎，终究还是错付了")
    }

     //switch 是不是一样
    val res: String = str match {
      case "韩博" => "我是帅哥"
      case "小杨" => "我是美女"
      case "前进" => "我是富二代"
      case _ => "我是思聪"
    }
    println(res)


    val num: Int = new Random().nextInt(100)

    //守卫模式
    num match {
      case a if a > 90 => println(a+"优秀")
      case a if a > 80 => println(a+"良好")
      case a if a > 70 => println(a+"中")
      case a if a > 60 => println(a+"及格")
      case a if a > 30 => println(a+"不及格")
      case _ => println("废物")
    }

    println("============离别伤感得分隔符====================")

    //匹配类型
    val list: List[Any] = List(1, 'c', "jin", true, (10, "hehe"), 0.8,("10", "hehe"))
    for (elem <- list) {
      elem match {
        case a:Int => println(a * 10)
        case a:Char => println(a + 10)
        case a:String => println(a.toUpperCase())
        case a:Boolean => println(!a)
        case a:(Double,Double) => println("我是一个元组:"+a)//泛型得擦除
        case a:Double => println(a * 200)
        case _ => println("啥也不是！！！")
      }
    }


    val arrInt: Array[Int] = Array(1, 2, 3)
    val arrString: Array[String] = Array("1", "2", "3")
    val listString: List[String] = List("a", "b")
    val listInt: List[Int] = List(1,2,3)
    val set: Set[Int] = Set(1,2,3)

    println("===========走咯============")

    val list1: List[Any] = List(arrInt, arrString, listString, listInt,set)
    for (elem <- list1) {
          elem match {
            /*
            Int==>Array
            String==>Array
            Int==>List
            Int==>List
             */
            case a:Array[Int] => println("Int==>Array")
            case a:Array[String] => println("String==>Array")
            //泛型得擦除
            case a:List[Int] => println("Int==>List")
            case a:List[String] => println("String==>List")
            //泛型也被擦除了
            case a:Set[String] => println("abc")
          }
    }

    //匹配数组

    println("===========前进，你舍得我吗？====================")
    val array:  List[Any] =  List(1, 2, 3)
    val array1: List[Any] = List("1", "2", 3)
    val array2: List[Any] = List("1", 2, 3)
    val array3: List[Any] = List(1, 2, 3,4)
    val array4: List[Any] = List(1, 2, 3,4,5)
    val array5: List[Any] = List(1, 2)

    //匹配数组，匹配集合==》 上面说的是匹配类型
    val arrays: List[List[Any]] = List(array, array1, array2, array3, array4, array5)
    for (elem <- arrays) {
      elem match {
        case List(1,_*) => println("第一个元素是1得数组，具体有多少个无所谓")
        case List(1,a,b,_) => println("总共有4个元素得数组，而且四个元素中第一个元素必须是1"+a+b)
        case List(_,_,_,_) => println("总共有4个元素得数组")
        case List(1,_) => println("总共有2个元素得数组，而且四个元素中第一个元素必须是1")
        case List(_,2,_) => println("总共有3个元素得数组，而且2个元素中第一个元素必须是2")
        case List("1",_) => println("总共有2个元素得数组，而且四个元素中第一个元素必须是字符串1")
        case _ => println("漏网之鱼")
      }
    }


    for (elem <- List((1, 2), (1, 2, 3),(1,"2",3), (1, 2, 3, 4), (1, "2", 3,"4") )) {
      elem match {
        case (_,_) => println("匹配一个2元元组")
        case (_,"2",_) => println("匹配一个3元元组,并且第二个元素是字符串2")
        case (_,_,_,"4") => println("匹配一个4元元组，第四个元素是字符串4")
        case (_,_,_,_) => println("匹配一个4元元组")
        case (_,_,_) => println("匹配一个3元元组")
        case _ => println("漏网之鱼")
      }
    }

    val map: Map[Int, String] = Map((1, "zss"), (2, "lss"))
    //解构匹配
    for ((a,b) <- map) {

    }

    val (a,b) = (1,"zss")//==>
    //类似于
    val a1 = 1
    val b1 = "zss"

    //解构匹配  匹配对象 重写了unapply方法得
  /*  val aaa: AAA = AAA(1,"zss","male")
    val aaa1: AAA = AAA(2,"lss","male")
    val aaa2: AAA = AAA(3,"www","male")
    val aaa3: AAA = AAA(4,"zll","male")
    val aaa4: AAA = AAA(5,"tqq","male")

    val list8: List[AAA] = List(aaa, aaa1, aaa2, aaa3, aaa4)
    for (elem <- list8) {
      elem match {
        case AAA(1,_,_) => println("id为1得人")
        case AAA(id,"lss",_) => println(s"匹配名字为lss得人,他的id是：$id")
        case AAA(_,_,"male") => println("id为1得人")
      }
    }*/


    val aaa:  BBB =  BBB(1,"zss","male")
    val aaa1: BBB =  BBB(2,"lss","male")
    val aaa2: BBB =  BBB(3,"www","male")
    val aaa3: BBB =  BBB(4,"zll","male")
    val aaa4: BBB =  BBB(5,"tqq","male")

    val list8: List[BBB] = List(aaa, aaa1, aaa2, aaa3, aaa4)
    for (elem <- list8) {
      elem match {
        case BBB(1,_,_) => println("id为1得人")
        case BBB(id,"lss",_) => println(s"匹配名字为lss得人,他的id是：$id")
        case BBB(_,_,"male") => println("id为1得人")
      }
    }



  }
}

class AAA(val id:Int,val name:String,val gender:String)


object AAA{
  def apply(id: Int, name: String, gender: String): AAA = new AAA(id, name, gender)
  def unapply(arg: AAA): Option[(Int, String, String)] = Some(arg.id,arg.name,arg.gender)
}


case class BBB(val id:Int,val name:String,val gender:String)